package com.aaa.jur.controller;


import com.aaa.common.entity.LayUiTable;
import com.aaa.common.entity.Role;
import com.aaa.jur.service.RoleService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author AAATeacherChen
 * @since 2020-06-14
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @ResponseBody
    @RequestMapping("/selectAll")
    public List<Role> selectAll() {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0);
        List<Role> list = roleService.selectList(wrapper);
        System.out.println(list);
        return list;
    }

    @RequestMapping("/roleList")
    public String toRole() {
        return "showRole";
    }

    @RequestMapping("/selectAllRole")
    @ResponseBody
    public LayUiTable selectRole() {
        List<Role> list = roleService.selectList(null);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("成功");
        layUiTable.setCount(0);
        layUiTable.setData(list);
        return layUiTable;
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Role selectById(Integer id) {
        Role role = roleService.selectById(id);
        return role;
    }

    @RequestMapping("/deleteRole")
    @ResponseBody
    public LayUiTable deleteRole(Integer id) {
        boolean b = roleService.deleteById(id);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("成功");
        layUiTable.setCount(0);
        layUiTable.setData(null);
        if (b) {
            layUiTable.setMsg("成功");
        } else {
            layUiTable.setMsg("失败");
        }
        return layUiTable;
    }

    @RequestMapping("/saveRole")
    @ResponseBody
    public LayUiTable saveRole(Role role) {
        boolean b = roleService.insert(role);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setCount(0);
        if (b) {
            layUiTable.setMsg("成功");
            layUiTable.setData(role.getRoleName());
        } else {
            layUiTable.setData(null);
            layUiTable.setMsg("失败");
        }
        return layUiTable;
    }

    @RequestMapping("/updateRole")
    @ResponseBody
    public LayUiTable updateRole(Role role) {
        boolean b = roleService.updateById(role);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setCount(0);
        if (b) {
            layUiTable.setMsg("成功");
            layUiTable.setData(role.getRoleName());
        } else {
            layUiTable.setData(null);
            layUiTable.setMsg("失败");
        }
        return layUiTable;
    }

}

